public class Solution {
    public static int[][] generateMatrix(int n) {
        int[][] array = new int[n][n];
        int startx = 0,  starty = 0;
        int offset = 1;
        int count = 1;
        int  i = 0, j= 0;
        int ret = n / 2;
        while(ret > 0) {
            for (j = starty; j < n - offset; j++) {
                array[startx][j] = count++;
            }
            for (i = startx; i < n - offset; i++) {
                array[i][j] = count++;
            }
            for (;j > starty; j--) {
                array[i][j] = count++;
            }
            for (;i > startx; i--) {
                array[i][j] = count++;
            }
            startx++;
            starty++;
            offset++;
            ret--;
        }
        if (n % 2 == 1) {
            array[startx][starty] = count;
        }
        return array;
    }

    public static void main(String[] args) {
        int[][] ret = generateMatrix(3);

    }
}